<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('区域管理')"/>
    <th:block th:include="include :: layout-latest-css"/>
    <th:block th:include="include :: ztree-css"/>
    <th:block th:include="include :: datetimepicker-css"/>
</head>
<style>
    input::-webkit-outer-spin-button, input::-webkit-inner-spin-button {
        -webkit-appearance: none;
    }

    input[type="number"] {
        -moz-appearance: textfield;
    }

    .gray-bg {
        background-color: #FFFFFF;
    }

</style>
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content" id="dv">
    <form class="form-horizontal m" id="basic-add" AUTOCOMPLETE="OFF">
        <input type="hidden" id="companyId" name="companyId">
        <input type="hidden" id="proId" name="proId">
        <input type="hidden" id="programId" name="programId">
        <h4 class="form-header h4">基本信息</h4>
        <div class="form-group">
            <div class="col-sm-12">
                <div class="col-sm-6">
                    <label class="col-sm-4 control-label">产值口径：</label>
                    <div class="col-sm-8">
                        <select name="outputCaliber" id="outputCaliber" class="form-control noselect2" required>
                            <option value="">请选择</option>
                            <option value="0">存</option>
                            <option value="1">增1</option>
                            <option value="2">增2</option>
                            <option value="3">增3</option>
                            <option value="4">增4</option>
                        </select>
                    </div>
                </div>
                <div class="col-sm-6">
                    <label class="col-sm-4 control-label">中标日期：</label>
                    <div class="col-sm-8">
                        <input id="bidTime" name="bidTime" class="form-control" type="text" placeholder="yyyy-mm-dd"
                               required>
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-12">
                <div class="col-sm-6">
                    <label class="col-sm-4 control-label">状态：</label>
                    <div class="col-sm-8">
                        <select name="status" id="status" class="form-control noselect2" required>
                            <option value="">请选择</option>
                            <option value="0">已竣已结</option>
                            <option value="1">已竣未结</option>
                            <option value="2">竣工准备</option>
                            <option value="3">在建</option>
                            <option value="4">待建</option>
                        </select>
                    </div>
                </div>
                <div class="col-sm-6">
                    <label class="col-sm-4 control-label">模式：</label>
                    <div class="col-sm-8">
                        <select name="pattern" id="pattern" class="form-control noselect2" required>
                            <option value="">请选择</option>
                            <option value="0">对内</option>
                            <option value="1">对外</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group" shiro:hasRole="admin">
            <div class="col-sm-12">
                <div class="col-sm-6">
                    <label class="col-sm-4 control-label">中标价(万)：</label>
                    <div class="col-sm-8">
                        <input id="bidPrice" name="bidPrice" class="form-control" type="text"
                               placeholder="请输入中标价" required>
                    </div>
                </div>
                <div class="col-sm-6">
                    <label class="col-sm-4 control-label">预估结算价(万)：</label>
                    <div class="col-sm-8">
                        <input id="preSettlePrice" name="preSettlePrice" class="form-control" type="number"
                               placeholder="请输入预估结算价" required>
                    </div>
                </div>
            </div>
        </div>
        <input id="accFinishPrice" name="accFinishPrice" type="hidden">
        <input id="countOffset" name="countOffset" type="hidden">
        <h4 class="form-header h4 schedule">工期（0）天</h4>
        <input type="hidden" id="schedule" name="schedule">
        <div class="form-group" style="white-space: nowrap;">
            <div class="col-sm-12">
                <div class="col-sm-6">
                    <label class="col-sm-4 control-label">实际开工时间：</label>
                    <div class="col-sm-8">
                        <input id="actualStartTime" name="actualStartTime" class="form-control" placeholder="yyyy-mm-dd"
                               type="text" required>
                    </div>
                </div>
                <div class="col-sm-6">
                    <label class="col-sm-4 control-label">计划结工时间：</label>
                    <div class="col-sm-8">
                        <input id="planFinishTime" name="planFinishTime" class="form-control" placeholder="yyyy-mm-dd"
                               type="text" required>
                    </div>
                </div>
            </div>
        </div>
        <h4 class="form-header h4"></h4>
        <div class="form-group" style="white-space: nowrap;">
            <div class="col-sm-12">
                <div class="col-sm-6">
                    <label class="col-sm-4 control-label">项目经理：</label>
                    <div class="col-sm-8">
                        <input id="projectManager" name="projectManager" class="form-control" placeholder="请输入项目经理"
                               type="text" required>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>


<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: select2-js"/>

<script th:inline="javascript">
    var companyId = [[${companyId}]];
    var proId = [[${proId}]];
    var programId = [[${programId}]];
    var prefix = ctx + "planbudget";
    $(function () {
        $("#companyId").val(companyId);
        $("#proId").val(proId);
        $("#programId").val(programId);
        formatDate();
        $.ajax({
            url: prefix + '/queryBudgeByProgramId',
            data: {
                programId: programId
            },
            type: 'POST',
            success: function (data) {
                $("#accFinishPrice").val(Number(data.prePrice) + Number(data.accFinish));
                $("#countOffset").val(Number($("#accFinishPrice").val()) / Number($("#preSettlePrice").val()));
            }
        });

        $("input").blur(function () {
            setTimeout(function () {
                var time = (getDate($("#planFinishTime").val()) - getDate($("#actualStartTime").val())) / (1000 * 60 * 60 * 24);
                if (!isNaN(time)) {
                    if ((getDate($("#planFinishTime").val()) - getDate($("#actualStartTime").val())) / (1000 * 60 * 60 * 24) < 0) {
                        $("#planFinishTime").val("");
                        $.modal.msgWarning('结工时间必须大于开工时间！');
                        return false
                    } else {
                        $("#schedule").val(time);
                        $(".schedule").text("工期（" + time + "）天");
                    }

                }
            }, 100)
        })
    })


    function getDate(date) {
        var time = date;
        time = time.replace(/-/g, '/');
        return new Date(time)
    }


    function formatDate() {
        layui.use('laydate', function () {
            var laydate = layui.laydate;
            laydate.render({
                elem: '#bidTime',
            });
            laydate.render({
                elem: '#actualStartTime',
            });
            laydate.render({
                elem: '#planFinishTime',
            });
        });
    }

    function submitHandler() {
        $.ajax({
            url: prefix + '/queryBasicByProgramId',
            data: {
                programId: programId
            },
            type: 'POST',
            success: function (data) {
                if (data.length >= 1) {
                    $.modal.msgWarning('项目已存在！');
                    return false
                } else {
                    if ($.validate.form()) {
                        var data = $("#basic-add").serializeArray();
                        $.operate.save(prefix + '/saveBasic', data);
                        parent.refresh();
                    }

                }
            }
        })

    }

</script>
</body>
</html>